﻿function AT2_ReplacesPanelUI_Setup()
	AT2_UI_Replaces:SetBackdropColor(0, 0, 0, 0.85)
	
	AT2_UI_ResizeHandler()
	
	StaticPopupDialogs["AT2_NewReplace"] = {
	text = "Add meg a helyettesítés \124cffFFFF00nevét\124r.\nA %név% -al fogod tudni használni az üzenetben.",
	button1 = "Ok",
	button2 = "Mégse",
	whileDead = true,
	hideOnEscape = true,
	timeout = 0,
	enterClicksFirstButton = true,
	hasEditBox = true,
	
	EditBoxOnTextChanged = function (self, data)
		if not(self:GetText() == "") then
			local foundReplaceWithSameName=false
			for i=1,#AT2_Save["Replaces"] do
				if select(1, strsplit("\a",AT2_Save["Replaces"][i]))==self:GetText() then
					foundReplaceWithSameName=true
					AT2_Print("foundReplaceWithSameName="..tostring(foundReplaceWithSameName).." text: "..self:GetText().." - currentsav: "..select(1, strsplit("\a",AT2_Save["Messages"][i])),"debug","new replace staticpopup")
				else
					AT2_Print("foundReplaceWithSameName="..tostring(foundReplaceWithSameName).." text: "..self:GetText().." - currentsav: "..select(1, strsplit("\a",AT2_Save["Messages"][i])),"debug","new replace staticpopup")
				end
			end
			if (foundMessageWithSameName==true) then
				self:GetParent().button1:Disable()
			else
				self:GetParent().button1:Enable()
			end
		else
			self:GetParent().button1:Disable()
		end
		
	end,
	OnAccept = function (self, data) 
		AT2_EnableButton(AT2_UI_Replaces_List)
		AT2_EnableButton(AT2_UI_Replaces_Delete)
		AT2_EnableButton(AT2_UI_Replaces_Insert)
		AT2_WriteReplace("create",nil,"--nincsadat--",self.editBox:GetText())
		AT2_SelectedReplace=#AT2_Save["Replaces"]
		AT2_UI_Replaces_EditBox:SetText("")
		AT2_UI_Replaces_EditBox:SetFocus()
		AT2_UI_Replaces_Name:SetText(AT2_ReadReplace("name",AT2_SelectedReplace))
	end,
	OnShow = function (self)
		self.button1:Disable()
	end,
	}
	
	function AT2_ReadReplace(type, which) 
		if (type=="name" or type=="title") then
			return select(1, strsplit("\a", AT2_Save["Replaces"][which]))
		elseif (type=="code") then                         
			return select(2, strsplit("\a", AT2_Save["Replaces"][which]))
		elseif (type=="all") then                            
			return select(1, strsplit("\a", AT2_Save["Replaces"][which])),select(2, strsplit("\a", AT2_Save["Replaces"][which]))
		else
			AT2_Print("Helyettesítés olvasására érvénytelen típus lett kiválasztva. Részletek: T"..type.." W"..which,"error" )
		end
	end
	
	function AT2_WriteReplace(type, which, text, title)
		if not(which == 0) then
			if (type=="name") then
				AT2_Save["Replaces"][which]=text.."\a"..select(1,strsplit("\a",AT2_Save["Replaces"][which]))
			elseif (type=="msg") then
				AT2_Save["Replaces"][which]=select(1,strsplit("\a",AT2_Save["Replaces"][which])).."\a"..text
			elseif (type=="create") then
				AT2_Save["Replaces"][#AT2_Save["Replaces"]+1]="X\a"..title.."\a"..text
			else
			type=tostring(type);which=tostring(which);text=tostring(text)
				AT2_Print("Helyettesítés írására érvénytelen típus lett kiválasztva. Részletek: T"..which.." W"..which.." T"..text,"error" )
			end
		else
			AT2_Print("Az összes helyettesítés törölve lett? Írni próbált a 0. mentett üzenetre.","debug","AT2_WriteReplace, Type"..type..", Which"..which..", Text"..text)
		end
	end
	
	if not(AT2_ModuleList) then
		AT2_ModuleList = {}
	end
	tinsert(AT2_ModuleList,"ReplacesPanel");
end

